<?php
namespace app\admin\model;
use think\Db;
use think\Model;
use app\admin\model\RegionModel;

class Franchisee extends Model
{
     protected $region_model;
	 public function __construct()
	{
		parent::__construct();
		$this->region_model = new RegionModel();
	}

	//加盟商列表
    public function franchiseeList($page,$limit,$data){
        $where = [];
        $where[] = "is_del = 1";
        if (!empty($data['franchisee_name'])) {
            $where[] = "franchisee_name like '%" . rtrim($data['franchisee_name']) . "%'";
        }
        $whereStr = '';
        if ($where) {
            $whereStr = implode($where, ' and ');
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('franchisee')->where($whereStr)->count();
        $field = 'id,franchisee_name,organization,username,phone,province_id,city_id,content';
        $list = Db::name('franchisee')->where($whereStr)->limit($start, $limit)->field($field)->order('id','desc')->select();
        $provinceInfo = $cityInfo = $result = [];
        foreach($list as $key=>$value){
            $provinceInfo   = $this->region_model->getRegion(['region_id'=>$value['province_id']],'region_name');
            $cityInfo       = $this->region_model->getRegion(['region_id'=>$value['city_id']],'region_name');
            $list[$key]['addressName'] = $provinceInfo[0]['region_name'].$cityInfo['0']['region_name'];
            $result = $this->franchiseeCount($value['id']);
            //print_r($result);
            //推广人数
            $list[$key]['extendNum'] = $result['extendNum']?$result['extendNum']:0;
            //推荐人数
            $list[$key]['recomdNum'] = $result['recomdNum']?$result['recomdNum']:0;
            //顺风车认证人数
            $list[$key]['frAuthNum'] = $result['frAuthNum']?$result['frAuthNum']:0;
            //下单人数
            $list[$key]['orderNum']  = $result['orderNum']?$result['orderNum']:0;
            //订单数
            $list[$key]['ordersnNum']= $result['ordersnNum']?$result['ordersnNum']:0;
            //订单总额
            $list[$key]['orderMoney']= $result['orderMoney']?$result['orderMoney']:0.00;
            //转换率
            $list[$key]['switchNum'] = $result['switchNum'];
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }

    //加盟商查询数据信息
    public function franchiseeCount($franchisee_id){
        //查询推广员数量
        $result = $tgList = $referList = [];
        $result['extendNum'] = DB::name('refer')->where(['fid'=>$franchisee_id,'status'=>1,'is_del'=>1])->count();//推广员数量
        //查询推广员
        $tgList = DB::name('refer')->where(['fid'=>$franchisee_id,'status'=>1,'is_del'=>1])->select();
        $strUid = '';
        //推广员拼凑成字符串
        foreach ($tgList as $key=>$value){
            $strUid .= $value['uid'].',';
        }
        if(!empty($strUid)){
            $referList = DB::name('user')->where('referee_id','in',rtrim($strUid,','))->where(['is_del'=>1])->field('uid,user_phone')->select();
        }
        //推荐人数统计
        $result['recomdNum'] = count($referList);
        //判断推荐人数是否为空
        if(!empty($referList)){
            //推荐人数不为空 计算下单人数 、 订单数 、 订单总额
            $userStr = '';
            foreach ($referList as $key=>$value){
                $userStr .= $value['uid'].',';
            }
            $userStr = rtrim($userStr,',');
            //查询顺风车认证人数
            $result['frAuthNum'] = $this->frAuthNum($userStr);
            $orderSumInfo = $this->orderSumInfo($userStr);
            $result['orderNum']     = $orderSumInfo['orderNum'];//下单人数
            $result['ordersnNum']   = $orderSumInfo['ordersnNum'];//订单数
            $result['orderMoney']   = $orderSumInfo['orderMoney'];//订单总额
            //$result['switchNum']    = $orderSumInfo['switchNum'];//转化率

            $result['switchNum'] = floor(($orderSumInfo['orderNum']/$result['recomdNum'])*100).'%';

            //查询顺风车
            //$frOrderInfo = $this->frOrderInfo($userStr);
            //查询城际
            //$interOrderInfo = $this->interOrderInfo($userStr);
            //查询专车
            //$xmOrderInfo = $this->xmOrderInfo($userStr);
            //下单人数
            //$userInfo = array_unique(array_merge($frOrderInfo['userid'],$interOrderInfo['userid'],$xmOrderInfo['userid']));
            //$result['orderNum'] = count($userInfo);
            //订单数量
            //$result['ordersnNum'] = $frOrderInfo['number'] + $interOrderInfo['number'] + $xmOrderInfo['number'];
            //订单总额
            //$result['orderMoney'] = $frOrderInfo['need_price'] + $interOrderInfo['need_price'] + $xmOrderInfo['need_price'];
            //转化率
            //$result['switchNum'] = floor(($result['orderNum']/$result['recomdNum'])*100).'%';
        }else{
            //推荐人数为空
            $result['frAuthNum'] = '0';//顺风认证人数
            $result['orderNum'] = '0';//下单人数
            $result['ordersnNum'] = '0';//订单数
            $result['orderMoney'] = '0';//订单总额
            $result['switchNum'] = '0%';//转化率
        }
        return $result;
    }

    //查询顺风车认证人数
    public function frAuthNum($userStr){
        return DB::table('fr_driver_base')->where('driver_uid','in',$userStr)->where(['auth_status'=>1])->count();
    }

    //计算数据
    public function orderSumInfo($userStr=''){
        //查询顺风车
        $frOrderInfo = self::frOrderInfo($userStr);
        //查询城际
        $interOrderInfo = self::interOrderInfo($userStr);
        //查询专车
        $xmOrderInfo = self::xmOrderInfo($userStr);
        //下单人数
        $userInfo = array_unique(array_merge($frOrderInfo['userid'],$interOrderInfo['userid'],$xmOrderInfo['userid']));
        $result['orderNum'] = count($userInfo);
        //订单数量
        $result['ordersnNum'] = $frOrderInfo['number'] + $interOrderInfo['number'] + $xmOrderInfo['number'];
        //订单总额
        $result['orderMoney'] = $frOrderInfo['need_price'] + $interOrderInfo['need_price'] + $xmOrderInfo['need_price'];
        //转化率
        $result['switchNum'] = floor(($result['orderNum']/$result['recomdNum'])*100).'%';
        return $result;
    }

    //查询顺风订单数量、订单总额
    public function frOrderInfo($userStr=''){
        $frOrderList = DB::table('fr_order_common')->where('uid','in',$userStr)->where(['order_status'=>['in',['45','50']],'is_del'=>2])->field('common_id,uid,need_price')->select();
        $result = $userId = [];
        $need_price = '';
        foreach ($frOrderList as $key=>$value){
            $need_price += $value['need_price'];          //计算金额
            $userId[] = $value['uid'];
        }
        $result['need_price']   = $need_price/100;
        $result['number']       = count($frOrderList);
        $result['userid']       = array_unique($userId);
        return $result;
    }

    //查询城际订单数量、订单总额
    public function interOrderInfo($userStr=''){
        $interOrderList = DB::table('inter_order_common')->where('member_id','in',$userStr)->where(['order_status'=>['in',['45','50']],'is_del'=>2])->field('id,member_id,need_price')->select();
        $result = $userId = [];
        $need_price = '';
        foreach ($interOrderList as $key=>$value){
            $need_price += $value['need_price'];          //计算金额
            $userId[] = $value['member_id'];
        }
        $result['need_price']   = $need_price/100;
        $result['number']       = count($interOrderList);
        $result['userid']       = array_unique($userId);;
        return $result;
    }
    //查询专车打车数量、订单总额
    public function xmOrderInfo($userStr=''){
        $xmOrderList = DB::name('order_common')->where('uid','in',$userStr)->where(['order_status'=>['in',['45','50']],'is_del'=>2])->field('common_id,uid,need_price')->select();
        $result = $userId = [];
        $need_price = '';
        foreach ($xmOrderList as $key=>$value){
            $need_price += $value['need_price'];          //计算金额
            $userId[] = $value['uid'];
        }
        $result['need_price']   = $need_price/100;
        $result['number']       = count($xmOrderList);
        $result['userid']       = array_unique($userId);;
        return $result;
    }
}
